﻿using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;

namespace WeatherSystem
{
    /// <summary>
    /// 天气 Feature
    /// </summary>
    public class WeatherFeature : ScriptableRendererFeature
    {
        private WeatherRenderPass _pass;

        public override void Create()
        {
            _pass = new WeatherRenderPass();
            _pass.renderPassEvent = RenderPassEvent.BeforeRenderingPostProcessing;
        }

        public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
        {
            renderer.EnqueuePass(_pass);
        }
    }
    
    public class WeatherRenderPass : ScriptableRenderPass
    {
        public override void OnCameraSetup(CommandBuffer cmd, ref RenderingData renderingData)
        {
        }

        public override void Configure(CommandBuffer cmd, RenderTextureDescriptor cameraTextureDescriptor)
        {
        }

        public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
        {
            var stack = VolumeManager.instance.stack;
            var volume = stack.GetComponent<WeatherVolume>();

            if (RenderSettings.sun != null)
            {
                RenderSettings.sun.color = volume.sunColor.value;
                RenderSettings.sun.intensity = volume.sunColorIntensity.value;
                RenderSettings.ambientSkyColor = volume.ambientColor.value;
            }
        }

        public override void OnCameraCleanup(CommandBuffer cmd)
        {
        }
    }
}
